﻿/*  ----------------------------------------------------------------------------
 *  Warren Moxley
 *  ----------------------------------------------------------------------------
 *  GTasksSync
 *  ----------------------------------------------------------------------------
 *  File:       Util.cs
 *  Author:     IT1SERVICES\wmoxley
 *  ----------------------------------------------------------------------------
	Licensed to the Apache Software Foundation (ASF) under one
       	or more contributor license agreements.  See the NOTICE file
       	distributed with this work for additional information
       	regarding copyright ownership.  The ASF licenses this file
       	to you under the Apache License, Version 2.0 (the
       	"License"); you may not use this file except in compliance
       	with the License.  You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

       	Unless required by applicable law or agreed to in writing,
       	software distributed under the License is distributed on an
       	"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
       	KIND, either express or implied.  See the License for the
       	specific language governing permissions and limitations
       	under the License. 
 *  ----------------------------------------------------------------------------
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security;
using System.Security.Cryptography;

namespace GTasksSync.Util
{
    public static class Encryption
    {
        public static string EncryptString(string toEncrypt, string bkey)
        {
            byte[] keyArray;
            byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);

            MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
            keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(bkey));

            TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
            tdes.Key = keyArray;
            tdes.Mode = CipherMode.ECB;
            tdes.Padding = PaddingMode.PKCS7;

            ICryptoTransform cTransform = tdes.CreateEncryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

            return Convert.ToBase64String(resultArray, 0, resultArray.Length);

        }

        public static string EncryptString(int value, string bkey)
        {
            return EncryptString(value.ToString(), bkey);

        }

        public static string DecryptString(string toDecrypt, string bkey)
        {
            byte[] keyArray;
            byte[] toEncryptArray = Convert.FromBase64String(toDecrypt);

            MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
            keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(bkey));

            TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
            tdes.Key = keyArray;
            tdes.Mode = CipherMode.ECB;
            tdes.Padding = PaddingMode.PKCS7;

            ICryptoTransform cTransform = tdes.CreateDecryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

            return UTF8Encoding.UTF8.GetString(resultArray);
        }


    }

}
